package org.dtrd.modules.evaluation.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.dtrd.base.BaseController;
import org.dtrd.common.api.vo.Result;
import org.dtrd.modules.base.service.BaseCommonService;
import org.dtrd.modules.evaluation.entity.bean.AngiocardiopathyInfo;
import org.dtrd.modules.evaluation.entity.po.DtrdEntEvaluationRdAngiocardiopathy;
import org.dtrd.modules.evaluation.entity.request.AngiocardiopathyRequest;
import org.dtrd.modules.evaluation.service.IDtrdEntEvaluationRdAngiocardiopathyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import static org.dtrd.common.constant.CommonConstant.*;
import static org.dtrd.config.system.SystemState.ResponseState.STATE_OPERATION_FAILURE;
import static org.dtrd.config.system.SystemState.ResponseState.STATE_PARAMS_MISSING;

/**
 * @Author: QR
 * @Date: 2021/9/27-15:28
 */
@Api(tags = "量表评估")
@RestController
public class AngiocardiopathyController extends BaseController {

    @Autowired
    private IDtrdEntEvaluationRdAngiocardiopathyService angiocardiopathyService;
    @Autowired
    private BaseCommonService baseCommonService;

//    @ApiOperation("保存心血管10年风险评估量表记录")
//    @PostMapping(value = {"/105/25/1052501", "100/17/1001709"})
//    public Result<?> saveAngiocardiopathyInfo(@RequestBody AngiocardiopathyDTO dto){
//        // 校验参数
//        boolean paramMissing = dto.getBirth() == null || dto.getGender() == null
//                || dto.getIsSmoke() == null || dto.getIsDiabetes() == null || dto.getPressureSystolic() == null
//                || dto.getTc() == null || dto.getWeight() == null || dto.getHeight() == null;
//        if (paramMissing) {
//            return Result.error(STATE_PARAMS_MISSING.getCode(), STATE_PARAMS_MISSING.getMsg());
//        }
//        boolean notValidParams = dto.getGender() <= 0 || dto.getPressureSystolic() <= 0 || dto.getHeight() <= 0
//                || dto.getWeight() <= 0 || dto.getTc() <= 0 || dto.getIsSmoke() <= 0 || dto.getIsDiabetes() <= 0;
//        if (notValidParams) {
//            return Result.error(STATE_PARAMS_ERROR.getCode(), STATE_PARAMS_ERROR.getMsg());
//        }
//        AngiocardiopathyInfo angiocardiopathyInfo = angiocardiopathyService.saveAngiocardiopathyInfo(dto);
//        String logContent = String.format("保存心血管10年风险评估量表记录，用户id：%d", dto.getPatientId());
//        baseCommonService.addLog(logContent, OPERATE_TYPE_SAVE_OR_UPDATE);
//        return Result.OK(angiocardiopathyInfo);
//    }

    @ApiOperation(value = "保存心血管10年风险评估量表记录", notes = "新的， 所有都填选项")
    @PostMapping(value = "100/17/10017092")
    public Result<?> saveAngiocardiopathyInfo2(@RequestBody AngiocardiopathyRequest dto){
        // 校验参数
        if (isIntParamInvalid(dto.getPatientId()) || dto.getInput().size() != 7) {
            return error(STATE_PARAMS_MISSING);
        }
        AngiocardiopathyInfo angiocardiopathyInfo = angiocardiopathyService.saveAngiocardiopathyInfo(dto);
        String logContent = String.format("保存心血管10年风险评估量表记录，用户id：%d", dto.getPatientId());
        baseCommonService.addLog(logContent, OPERATE_TYPE_SAVE_OR_UPDATE);
        return Result.OK(angiocardiopathyInfo);
    }

    @ApiOperation("获取最新的心血管10年风险评估量表记录")
    @GetMapping("/105/25/1052502")
    public Result<?> getAngiocardiopathyInfoLatest(@RequestParam Integer patientId) {
        if (isIntParamInvalid(patientId)) {
            return Result.error(STATE_PARAMS_MISSING.getCode(), STATE_PARAMS_MISSING.getMsg());
        }
        AngiocardiopathyInfo angiocardiopathyInfo = angiocardiopathyService.getAngiocardiopathyInfoLatest(patientId);
        String logContent = String.format("获取最新的心血管10年风险评估量表记录，查询用户id：%d", patientId);
        baseCommonService.addLog(logContent, OPERATE_TYPE_QUERY);
        return Result.OK(angiocardiopathyInfo);
    }

    @ApiOperation("获取心血管10年风险评估量表记录列表")
    @GetMapping("/105/25/1052503")
    public Result<?> getAngiocardiopathyList(@RequestParam Integer patientId){
        if (isIntParamInvalid(patientId)){
            return error(STATE_PARAMS_MISSING);
        }
        LambdaQueryWrapper<DtrdEntEvaluationRdAngiocardiopathy> queryWrapper = Wrappers.lambdaQuery(DtrdEntEvaluationRdAngiocardiopathy.class)
                .eq(DtrdEntEvaluationRdAngiocardiopathy::getPatientId, patientId)
                .orderByDesc(DtrdEntEvaluationRdAngiocardiopathy::getCreateTime);
        List<DtrdEntEvaluationRdAngiocardiopathy> poList = angiocardiopathyService.list(queryWrapper);
        List<AngiocardiopathyInfo> dtoList = new ArrayList<>(poList.size());
        if (!poList.isEmpty()){
            dtoList = poList.stream()
                    .map(po -> new AngiocardiopathyInfo().parseFromPo(po))
                    .collect(Collectors.toList());
        }
        String logContent = String.format("获取心血管10年风险评估量表记录列表，查询用户id：%d", patientId);
        baseCommonService.addLog(logContent, OPERATE_TYPE_QUERY);
        return Result.OK(dtoList);
    }

    @ApiOperation("删除心血管10年风险评估量表记录列表")
    @DeleteMapping("/105/25/1052504")
    public Result<?> removeAngiocardiopathyById(@RequestParam Integer dataId){
        if (isIntParamInvalid(dataId)){
            return error(STATE_PARAMS_MISSING);
        }
        boolean result = angiocardiopathyService.removeById(dataId);
        String logContent = String.format("删除心血管10年风险评估量表记录列表，删除量表id：%d", dataId);
        baseCommonService.addLog(logContent, OPERATE_TYPE_DELETE);
        return result ? Result.OK() : error(STATE_OPERATION_FAILURE);
    }
}
